namespace TfsExtensions.Build.Helper
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using System.Threading;
    using System.Xml;
    using System.Xml.XPath;

    /// <summary>
    /// Class provides methods to fetch resources from Resource Files.
    /// </summary>
    public sealed class ResourceHelper
    {
        /// <summary>
        /// Prevents a default instance of the <see cref="ResourceHelper"/> class from being created. 
        /// Initializes a new instance of the <see cref="ResourceHelper"/> class.
        /// </summary>
        private ResourceHelper()
        {
        }

        /// <summary>
        /// Gets the name of the image bitmap by.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="bitmapName">Name of the bitmap.</param>
        /// <returns>Bitmap Object</returns>
        public static Bitmap GetImageBitmapByName(Assembly assembly, string bitmapName)
        {
            Bitmap bitmapImage = null;

            if (!string.IsNullOrEmpty(bitmapName) && assembly != null)
            {
                string[] resourceNames = assembly.GetManifestResourceNames();
                if (resourceNames != null & resourceNames.Length > 0)
                {
                    foreach (string resourceName in resourceNames)
                    {
                        if (resourceName.ToUpperInvariant().IndexOf(
                                bitmapName.ToUpperInvariant(), 
                                StringComparison.OrdinalIgnoreCase) != -1)
                        {
                            Stream imageStream = assembly.GetManifestResourceStream(resourceName);
                            if (imageStream != null)
                            {
                                bitmapImage = new Bitmap(imageStream);
                                resourceNames = null;
                                return bitmapImage;
                            }
                        }
                    }
                }
            }

            return bitmapImage;
        }

        /// <summary>
        /// Gets the resource string from culture specific resource files.
        /// </summary>
        /// <param name="baseName">Name of the base.</param>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="assembly">The assembly.</param>
        /// <returns>The get resource string.</returns>
        public static string GetResourceString(string baseName, string keyName, Assembly assembly)
        {
            string value = string.Empty;
            if (assembly != null && !string.IsNullOrEmpty(keyName) && string.IsNullOrEmpty(baseName))
            {
                var resourceManager = new ResourceManager(baseName, assembly);
                value = resourceManager.GetString(keyName, Thread.CurrentThread.CurrentCulture);
            }

            return value;
        }

        /// <summary>
        /// Get XML embedded resource.
        /// </summary>
        /// <param name="assembly">Caller Assembly</param>
        /// <param name="xmlName">Full Name of the Resource (Including Namespace and Folder Name)</param>
        /// <returns>Embeddded XML resource</returns>
        public static IXPathNavigable GetXmlResourceByName(Assembly assembly, string xmlName)
        {
            XmlDocument document = null;
            try
            {
                if (!string.IsNullOrEmpty(xmlName) && assembly != null)
                {
                    string[] resourceNames = assembly.GetManifestResourceNames();
                    if (resourceNames != null & resourceNames.Length > 0)
                    {
                        foreach (string resourceName in resourceNames)
                        {
                            if (resourceName.Equals(xmlName, StringComparison.CurrentCultureIgnoreCase))
                            {
                                Stream xmlStream = assembly.GetManifestResourceStream(resourceName);
                                if (xmlStream != null)
                                {
                                    document = new XmlDocument();
                                    document.Load(xmlStream);
                                    xmlStream.Close();
                                    return document;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                document = null;
            }

            return document;
        }
    }
}